package com.song.boot.springstudy.netty.commhandler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

// 入站处理器：将字节转为字符串
// 解码器
public class StringDecoderCustomization extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        try {
            String message = in.toString(io.netty.util.CharsetUtil.UTF_8);
            ctx.fireChannelRead(message); // 传递给下一个处理器
        } finally {
            ReferenceCountUtil.release(msg); // 释放资源
        }
    }
}